﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using PesticideManage.API.Data;
using System.Diagnostics;

namespace PesticideManage.API.Extensions.DependencyInjection;

public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddPesticideContext(this IServiceCollection services)
    {
        var provider = services.BuildServiceProvider();
        var configuration = provider.GetService<IConfiguration>()!;
        IWebHostEnvironment environment = provider.GetService<IWebHostEnvironment>()!;
        string connectionString = configuration.GetConnectionString("PesticidesConnection")!;

        services.AddDbContext<PesticideContext>(options =>
        {
            if (environment.IsDevelopment())
            {
                options.UseSqlServer(connectionString);
                options.LogTo(message => Debug.WriteLine(message));
            }
            else
            {
                options.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 28)));
            }
            //options.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 28)));
            //options.UseSqlite("Data Source=Pesticides.db");
        });
        return services;
    }
}
